﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Function
{
    public partial class FunctionForm : Form
    {
        Form Index;
        bool isExit = true;

        public FunctionForm()
        {
            InitializeComponent();
        }

        public FunctionForm(Form Index)
        {
            InitializeComponent();
            this.Index = Index;
        }

        private void FunctionForm_Load(object sender, EventArgs e)
        {

        }

        private void FunctionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isExit) Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                listBox1.Items.Add(textBox1.Text);
                textBox1.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                textBox1.Text = "请先添加选项！";
                return;
            }
            // 重置随机数种子
            Random rand = new Random();
            rand.Next();
            // 随机选择一个选项
            int index = rand.Next(listBox1.Items.Count);
            string result = listBox1.Items[index].ToString();
            textBox1.Text = "抽取结果：" + result;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Index.Show();
            Index.Location = this.Location;
            isExit = false;
            this.Close();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 按下回车键，添加选项
            if (e.KeyChar == (char)13)
            {
                if (textBox1.Text != "" && textBox1.Text != "请先添加选项！" && textBox1.Text.Substring(0, 1) != "抽")
                    button1_Click(sender, e);
                else
                    button2_Click(sender, e);
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            // 删掉选中的选项
            listBox1.Items.Remove(listBox1.SelectedItem);
        }
    }
}
